//
//  OALoginViewController.m
//  LongOA
//
//  Created by 耿用强 on 15/7/4.
//  Copyright (c) 2015年 耿用强. All rights reserved.
//

#import "OALoginViewController.h"
#import "OAacountSetViewController.h"
#import "OALoginCell.h"
#import "OAIpdataBase.h"
#import "P1ClientCreator.h"
#import "OAAccount.h"

#import "GetTokenRequest.h"
#import "GetTokenResponse.h"

@interface OALoginViewController ()<UITableViewDataSource,UITableViewDelegate,OALoginCellDelegate>
{
    NSArray*_nameArray;
    NSArray*_ploadArray;
 
    NSString* IPString;
    NSString* PortString;
}
@end

@implementation OALoginViewController

- (void)initData
{
    _nameArray=@[@"直连IP",@"直连端口"];
    _ploadArray=@[@"请输入IP地址",@"请输入端口号"];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initData];
    
    NSArray*array=[OAIpdataBase getIpandPortData];
    if (array.count<=0) {
        [self setNavBarleftBtn:@"" andlDimg:@"" rightBtn:@"" andrImg:@"" title:@"龙嘉OA" ];
    }
    else
    {
        [self setNavBarleftBtn:@"Common/back_btn" andlDimg:@"" rightBtn:@"" andrImg:@"" title:@"龙嘉OA" ];
    }
    
    [self buildTables:CGRectMake(0, HEADER_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT-HEADER_HEIGHT) andHeaderView:[self headerView] andFooterView:[self footerView] andAlwaysBounceVertical:YES Delegate:self DataSouce:self];

    // Do any additional setup after loading the view.
}

- (UIView*)headerView
{
    UIView*header=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, RESIZE_UI(100)+RESIZE_UI(40))];
    
    UIImageView*_blackView=[[UIImageView alloc]initWithFrame:CGRectMake(RESIZE_UI(15), RESIZE_UI(100), header.width-RESIZE_UI(30), RESIZE_UI(40))];
    _blackView.layer.borderColor=[[UIColor lightGrayColor]CGColor];
    _blackView.layer.borderWidth=1;
    [header addSubview:_blackView];
    
    UIButton*socketBt=[UIButton buttonWithType:UIButtonTypeCustom];
    socketBt.frame=CGRectMake(0, 0, _blackView.width/2, _blackView.height);
    [socketBt setTitle:@"SOCKET直连" forState:UIControlStateNormal];
    [socketBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    socketBt.titleLabel.font=FONT_14;
    [_blackView addSubview:socketBt];
    
    UIButton*httpBt=[UIButton buttonWithType:UIButtonTypeCustom];
    httpBt.frame=CGRectMake(socketBt.right, 0, _blackView.width/2, _blackView.height);
    [httpBt setTitle:@"HTTP直连" forState:UIControlStateNormal];
    [httpBt setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    httpBt.titleLabel.font=FONT_14;
    [_blackView addSubview:httpBt];
    
    
    
    
    
    return header;
}

- (UIView*)footerView
{
    UIView*footer=[[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, RESIZE_UI(80)+RESIZE_UI(40))];
    
    UIButton*button=[UIButton buttonWithType:UIButtonTypeCustom];
    button.frame=CGRectMake(RESIZE_UI(15), RESIZE_UI(70), SCREEN_WIDTH-RESIZE_UI(30), RESIZE_UI(90/2));
    button.backgroundColor=RGBColor(40, 119, 193, 1);
    [button setTitle:@"获取账套" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button.titleLabel.font=FONT_16;
    [button  addTarget:self action:@selector(getToken) forControlEvents:UIControlEventTouchUpInside];
    [footer addSubview:button];
    return footer;

}
#pragma mark - UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString*logincellIdentifer=@"logincellIdentifer";
    OALoginCell*cell=[tableView dequeueReusableCellWithIdentifier:logincellIdentifer];
    if (cell==nil) {
        cell=[[OALoginCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:logincellIdentifer];
        cell.delegate=self;
    }
    [cell showName:_nameArray[indexPath.row] andPloadText:_ploadArray[indexPath.row] andIndex:indexPath];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return RESIZE_UI(44);
}


#pragma mark - LoginCellDelegate
- (void)loginCellTextFieldWithText:(NSString *)text andIndex:(NSIndexPath *)index
{
    switch (index.row) {
        case 0:
            IPString=text;
            break;
        case 1:
            PortString=text;
            break;
            
        default:
            break;
    }
}

#pragma mark - 获取帐套
- (void)getToken
{
    if (IPString.length==0) {
        [Commend showAlert:@"请输入ip地址"];
        return;
    }
    else if (PortString.length==0)
    {
        [Commend showAlert:@"请输入端口号"];
        return;
    }
    else
    {
//        NSMutableDictionary*_need=[[NSMutableDictionary alloc]init];
//        [_need setObject:@"" forKey:@""];
//        [_need setObject:@"" forKey:@""];
//        OAacountSetViewController*acountVc=[[OAacountSetViewController alloc]init];
//        [self.navigationController pushViewController:acountVc animated:YES];
//        return;
        id<ILJClientCreator> creator = [[P1ClientCreator alloc] initWithIP:IPString AndPort:[PortString intValue]];
        LJClientPool* pool = [[LJClientPool alloc] initWithCreator:creator AndSize:5];
        
        GetTokenRequest* request = [[GetTokenRequest alloc] init];
        
        [pool Doexecute:request
               RspClass:[GetTokenResponse class]
                 andMsg:@"获取账套中..."
                andView:nil
globalTimelinePostsWithBlock:^(id rsp) {
    GetTokenResponse* rslt = rsp;
    //NSLog(@"=====%@",rslt.accounts);
    int setid = [[OAIpdataBase shareInstance] AddServiceWithIP:IPString AndPort:[PortString intValue] AndAccounts:rslt.accounts];
    
//    NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
//    [dic setObject:[IPString mutableCopy] forKey:kIp];
//    [dic setObject:[PortString mutableCopy] forKey:kPort];
//    [dic setObject:rslt.accounts forKey:kaccounts];
//    [OAIpdataBase saveIpandPortData:dic];
    
    OAacountSetViewController*acountVc=[[OAacountSetViewController alloc]init];
    
    [self.navigationController pushViewController:acountVc animated:YES];
}];
        
//        [OANetPublic requestWithApiName:@"GetToken" params:nil andTag:0 andMsg:@"获取账套中..." andView:self.view globalTimelinePostsWithBlock:^(NSDictionary*result,NSError*error){
//            
//            if (!error) {
//                
//                NSMutableDictionary*dic=[[NSMutableDictionary alloc]init];
//                [dic setObject:[IPString mutableCopy] forKey:kIp];
//                [dic setObject:[PortString mutableCopy] forKey:kPort];
//                [dic setObject:result[@"accounts"] forKey:kaccounts];
//                [OAIpdataBase saveIpandPortData:dic];
//                OAacountSetViewController*acountVc=[[OAacountSetViewController alloc]init];
//                
//                [self.navigationController pushViewController:acountVc animated:YES];
//            }
//            else
//            {
//               
//            }
//        
//        }];
        
    }
}

- (void)leftBtnAction
{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
